package main

import (
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"github.com/axgle/mahonia"
	"log"
	"net/http"
	"os"
	"time"
)

//转换编码
//func Encoding(html string) string {
//	e,name:=DetermineEncoding(html)
//	if name!="utf-8"{
//		html=ConvertToStr(html,"gbk","utf-8")
//		e=unicode.UTF8
//	}
//	r := strings.NewReader(html)
//
//	utf8Reader := transform.NewReader(r, e.NewDecoder())
//	//将其他编码的reader转换为常用的utf8reader
//	all, _ := ioutil.ReadAll(utf8Reader)
//	log.Println(string(all))
//	return string(all)
//}
//
//func DetermineEncoding(html string) (encoding.Encoding,string) {
//	e, name, _ := charset.DetermineEncoding([]byte(html), "")
//	return e,name
//}

func main() {
	var url string
	//fmt.Scanln(&url)
	url = "https://www.xquledu.com/html/1/1192/"
	//创建客户端对象，发送请求
	var client = http.Client{}
	//发送请求
	req, _ := http.NewRequest("GET", url, nil)
	//req.Header.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)")
	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36")
	//发送请求
	// 发送请求
	resp, err := client.Do(req)
	// 处理异常
	if err != nil {
		fmt.Println("http get error", err)
		return
	}
	// 关闭流
	defer resp.Body.Close()
	doc, err := goquery.NewDocumentFromReader(resp.Body)
	if err != nil {
		log.Fatal(err)
	}
	//书名
	bookNmae := doc.Find("h1").Text()
	bookNmaeDecode := mahonia.NewDecoder("gbk").ConvertString(bookNmae)

	//创建导出文件
	file, err := os.Create(bookNmaeDecode+".txt")
	if err != nil {
		fmt.Println(err.Error())
	}
	defer file.Close()

	//写入书名
	file.WriteString(bookNmaeDecode+"\n")

	//作者
	authorNmae := doc.Find("h3").Text()
	authorNmaeDecode := mahonia.NewDecoder("gbk").ConvertString(authorNmae)
	file.WriteString(authorNmaeDecode+"\n")

	doc.Find("#list>dd").Each(func(i int, selection *goquery.Selection) {
		itemUrl,_ := selection.Find("a").Attr("href")

		//延时两秒
		time.Sleep(2*time.Second)
		//发送请求
		req, _ := http.NewRequest("GET", itemUrl, nil)
		//req.Header.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)")
		req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36")

		resp, err := client.Do(req)
		// 处理异常
		if err != nil {
			fmt.Println("http get error", err)
			return
		}
		// 关闭流
		defer resp.Body.Close()
		doc, err := goquery.NewDocumentFromReader(resp.Body)
		if err != nil {
			log.Fatal(err)
		}
		//写入章节名称
		chapterNmae := doc.Find("h1").Text()
		chapterNmaeDecode := mahonia.NewDecoder("gbk").ConvertString(chapterNmae)
		fmt.Println(chapterNmaeDecode)
		file.WriteString(chapterNmaeDecode+"\n")

		//写入章节内容
		contentTxt := doc.Find("#contents").Text()
		contentTxtDecode := mahonia.NewDecoder("gbk").ConvertString(contentTxt)
		file.WriteString(contentTxtDecode)

	})

	fmt.Print("程序执行完毕，请查看结果。")
}